package com.mdd.admin.service.order.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.service.order.IOperateHistoryService;
import com.mdd.admin.validate.order.OperateHistoryCreateValidate;
import com.mdd.admin.validate.order.OperateHistorySearchValidate;
import com.mdd.admin.validate.order.OperateHistoryUpdateValidate;
import com.mdd.admin.vo.order.OperateHistoryDetailVo;
import com.mdd.admin.vo.order.OperateHistoryListedVo;
import com.mdd.common.base.page.PageValidate;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.order.OperateHistory;
import com.mdd.common.mapper.order.OperateHistoryMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 订单操作历史记录实现类
 * @author LikeAdmin
 */
@Service
public class OperateHistoryServiceImpl implements IOperateHistoryService {

    @Resource
    OperateHistoryMapper operateHistoryMapper;

    /**
     * 订单操作历史记录列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<OrderOperateHistoryListedVo>
     */
    @Override
    public PageResult<OperateHistoryListedVo> list(PageValidate pageValidate, OperateHistorySearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<OperateHistory> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        operateHistoryMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:orderId@order_id:long",
            "=:operateMan@operate_man:str",
            "=:orderStatus@order_status:long",
            "=:note:str",
        });

        IPage<OperateHistory> iPage = operateHistoryMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<OperateHistoryListedVo> list = new LinkedList<>();
        for(OperateHistory item : iPage.getRecords()) {
            OperateHistoryListedVo vo = new OperateHistoryListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 订单操作历史记录详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return OrderOperateHistory
     */
    @Override
    public OperateHistoryDetailVo detail(Long id) {
        OperateHistory model = operateHistoryMapper.selectById(id);

        Assert.notNull(model, "订单还款计划线下支付记录数据不存在");

        OperateHistoryDetailVo vo = new OperateHistoryDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 订单操作历史记录新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Transactional
    @Override
    public void add(OperateHistoryCreateValidate createValidate) {
        OperateHistory model = new OperateHistory();
        model.setOrderId(createValidate.getOrderId());
        model.setOperateMan(createValidate.getOperateMan());
        model.setOrderStatus(createValidate.getOrderStatus());
        model.setNote(createValidate.getNote());
        operateHistoryMapper.insert(model);
    }

    /**
     * 订单操作历史记录编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Transactional
    @Override
    public void edit(OperateHistoryUpdateValidate updateValidate) {
        OperateHistory model = operateHistoryMapper.selectById(updateValidate.getId());

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setOrderId(updateValidate.getOrderId());
        model.setOperateMan(updateValidate.getOperateMan());
        model.setOrderStatus(updateValidate.getOrderStatus());
        model.setNote(updateValidate.getNote());
        operateHistoryMapper.updateById(model);
    }

    /**
     * 订单操作历史记录删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Transactional
    @Override
    public void del(Long id) {
        OperateHistory model = operateHistoryMapper.selectById(id);

        Assert.notNull(model, "数据不存在!");

        operateHistoryMapper.deleteById(id);
    }

}
